using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Microsoft.ProjectServer.QueueWatch
{
    public partial class Options : Form
    {
        private DataGridView dg = null;

        public Options()
        {
            InitializeComponent();
        }

        public DataGridView DataGrid
        {
            get { return dg; }
            set { dg = value; }
        }

        private void Options_Load(object sender, EventArgs e)
        {
            if (dg == null) return;

            foreach (DataGridViewColumn dc in dg.Columns)
            {
                if (dc.Name == "MessageType" || dc.Name == "SortOrder") continue;

                if (dc.Visible)
                {
                    lbDisplayedJobTypes.Items.Add(dc.Name);
                }
                else
                {
                    lbHiddenJobTypes.Items.Add(dc.Name);
                }
            }

            foreach (DataGridViewRow dr in dg.Rows)
            {
                if (dr.Visible)
                {
                    lbDisplayedMessageTypes.Items.Add(dr.Cells["MessageType"].Value);
                }
                else
                {
                    lbHiddenMessageTypes.Items.Add(dr.Cells["MessageType"].Value);
                }
            }

            btnAddJobType.Enabled = lbHiddenJobTypes.SelectedIndex > -1;
            btnRemoveJobType.Enabled = lbDisplayedJobTypes.SelectedIndex > -1;
            btnAddMsgType.Enabled = lbHiddenMessageTypes.SelectedIndex > -1;
            btnRemoveMsgType.Enabled = lbDisplayedMessageTypes.SelectedIndex > -1;
        }

        #region Button Event Handlers
        private void btnAddJobType_Click(object sender, EventArgs e)
        {
            int selectedIndex = lbHiddenJobTypes.SelectedIndex;
            lbDisplayedJobTypes.Items.Add(lbHiddenJobTypes.SelectedItem);
            lbHiddenJobTypes.Items.RemoveAt(lbHiddenJobTypes.SelectedIndex);
            if (lbHiddenJobTypes.Items.Count > 0 && selectedIndex < lbHiddenJobTypes.Items.Count)
            {
                lbHiddenJobTypes.SelectedIndex = selectedIndex;
            }
        }

        private void btnAddAllJobTypes_Click(object sender, EventArgs e)
        {
            for (int index = 0; index < lbHiddenJobTypes.Items.Count; index++)
            {
                lbDisplayedJobTypes.Items.Add(lbHiddenJobTypes.Items[index]);
            }
            lbHiddenJobTypes.Items.Clear();
        }

        private void btnRemoveAllJobTypes_Click(object sender, EventArgs e)
        {
            for (int index = 0; index < lbDisplayedJobTypes.Items.Count; index++)
            {
                lbHiddenJobTypes.Items.Add(lbDisplayedJobTypes.Items[index]);
            }
            lbDisplayedJobTypes.Items.Clear();
        }

        private void btnRemoveJobType_Click(object sender, EventArgs e)
        {
            int selectedIndex = lbDisplayedJobTypes.SelectedIndex;
            lbHiddenJobTypes.Items.Add(lbDisplayedJobTypes.SelectedItem);
            lbDisplayedJobTypes.Items.RemoveAt(lbDisplayedJobTypes.SelectedIndex);
            if (lbDisplayedJobTypes.Items.Count > 0 && selectedIndex < lbDisplayedJobTypes.Items.Count)
            {
                lbDisplayedJobTypes.SelectedIndex = selectedIndex;
            }
        }

        private void btnAddMsgType_Click(object sender, EventArgs e)
        {
            int selectedIndex = lbHiddenMessageTypes.SelectedIndex;
            lbDisplayedMessageTypes.Items.Add(lbHiddenMessageTypes.SelectedItem);
            lbHiddenMessageTypes.Items.RemoveAt(lbHiddenMessageTypes.SelectedIndex);
            if (lbHiddenMessageTypes.Items.Count > 0 && selectedIndex < lbHiddenMessageTypes.Items.Count)
            {
                lbHiddenMessageTypes.SelectedIndex = selectedIndex;
            }
        }

        private void btnAddAllMsgTypes_Click(object sender, EventArgs e)
        {
            for (int index = 0; index < lbHiddenMessageTypes.Items.Count; index++)
            {
                lbDisplayedMessageTypes.Items.Add(lbHiddenMessageTypes.Items[index]);
            }
            lbHiddenMessageTypes.Items.Clear();
        }

        private void btnRemoveAllMsgTypes_Click(object sender, EventArgs e)
        {
            for (int index = 0; index < lbDisplayedMessageTypes.Items.Count; index++)
            {
                lbHiddenMessageTypes.Items.Add(lbDisplayedMessageTypes.Items[index]);
            }
            lbDisplayedMessageTypes.Items.Clear();
        }

        private void btnRemoveMsgType_Click(object sender, EventArgs e)
        {
            int selectedIndex = lbDisplayedMessageTypes.SelectedIndex;
            lbHiddenMessageTypes.Items.Add(lbDisplayedMessageTypes.SelectedItem);
            lbDisplayedMessageTypes.Items.RemoveAt(lbDisplayedMessageTypes.SelectedIndex);
            if (lbDisplayedMessageTypes.Items.Count > 0 && selectedIndex < lbDisplayedMessageTypes.Items.Count)
            {
                lbDisplayedMessageTypes.SelectedIndex = selectedIndex;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            // Display and hide columns as selected.
            for (int index = 0; index < lbHiddenJobTypes.Items.Count; index++)
            {
                dg.Columns[lbHiddenJobTypes.Items[index].ToString()].Visible = false;
            }
            for (int index = 0; index < lbDisplayedJobTypes.Items.Count; index++)
            {
                dg.Columns[lbDisplayedJobTypes.Items[index].ToString()].Visible = true;
                dg.Columns[lbDisplayedJobTypes.Items[index].ToString()].DisplayIndex = index + 2;
            }
            // Display and hide rows as selected.
            int sortIndex;
            foreach (DataGridViewRow dr in dg.Rows)
            {
                sortIndex = lbDisplayedMessageTypes.FindStringExact(dr.Cells["MessageType"].Value.ToString());
                dr.Visible = (sortIndex > -1);
                dr.Cells["SortOrder"].Value = sortIndex;
            }
       }

        private void btnUpJob_Click(object sender, EventArgs e)
        {
            if (lbDisplayedJobTypes.SelectedIndex > 0)
            {
                string item = lbDisplayedJobTypes.SelectedItem.ToString();
                int index = lbDisplayedJobTypes.SelectedIndex;
                lbDisplayedJobTypes.Items.RemoveAt(index);
                lbDisplayedJobTypes.Items.Insert(index - 1, item);
                lbDisplayedJobTypes.SelectedIndex = index - 1;
            }
        }

        private void btnDownJob_Click(object sender, EventArgs e)
        {
            if (lbDisplayedJobTypes.SelectedIndex < lbDisplayedJobTypes.Items.Count-1)
            {
                string item = lbDisplayedJobTypes.SelectedItem.ToString();
                int index = lbDisplayedJobTypes.SelectedIndex;
                lbDisplayedJobTypes.Items.RemoveAt(index);
                lbDisplayedJobTypes.Items.Insert(index + 1, item);
                lbDisplayedJobTypes.SelectedIndex = index + 1;
            }
        }

        private void btnUpMsg_Click(object sender, EventArgs e)
        {
            if (lbDisplayedMessageTypes.SelectedIndex > 0)
            {
                string item = lbDisplayedMessageTypes.SelectedItem.ToString();
                int index = lbDisplayedMessageTypes.SelectedIndex;
                lbDisplayedMessageTypes.Items.RemoveAt(index);
                lbDisplayedMessageTypes.Items.Insert(index - 1, item);
                lbDisplayedMessageTypes.SelectedIndex = index - 1;
            }
        }

        private void btnDownMsg_Click(object sender, EventArgs e)
        {
            if (lbDisplayedMessageTypes.SelectedIndex < lbDisplayedMessageTypes.Items.Count - 1)
            {
                string item = lbDisplayedMessageTypes.SelectedItem.ToString();
                int index = lbDisplayedMessageTypes.SelectedIndex;
                lbDisplayedMessageTypes.Items.RemoveAt(index);
                lbDisplayedMessageTypes.Items.Insert(index + 1, item);
                lbDisplayedMessageTypes.SelectedIndex = index + 1;
            }
        }
        #endregion

        #region ListBox Event Handlers
        private void lbHiddenJobTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnAddJobType.Enabled = lbHiddenJobTypes.SelectedIndex > -1;
        }

        private void lbDisplayedJobTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnRemoveJobType.Enabled = lbDisplayedJobTypes.SelectedIndex > -1;
            btnUpJob.Enabled = lbDisplayedJobTypes.SelectedIndex > -1;
            btnDownJob.Enabled = lbDisplayedJobTypes.SelectedIndex > -1;
        }

        private void lbHiddenMessageTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnAddMsgType.Enabled = lbHiddenMessageTypes.SelectedIndex > -1;
        }

        private void lbDisplayedMessageTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnRemoveMsgType.Enabled = lbDisplayedMessageTypes.SelectedIndex > -1;
            btnUpMsg.Enabled = lbDisplayedMessageTypes.SelectedIndex > -1;
            btnDownMsg.Enabled = lbDisplayedMessageTypes.SelectedIndex > -1;
        }
        #endregion
    }
}